<template>
  <div>
    <el-card style="margin:20px 0px;">
      <CategorySelect @getCategoryId="getCategoryId" :show="!isShowTable"></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="isShowTable">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addAttr">添加属性</el-button>
        <!-- 表格展示 -->
        <el-table style="width: 100%" border :data="AttrList">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="150" align="center">
          </el-table-column>
          <el-table-column prop="prop" label="属性值列表" width="width">
            <template slot-scope="{row,$index}">
              <el-tag type="success" v-for="(attrValue, index) in row.attrValueList" :key="attrValue.id"
                style="margin:0px 20px">{{
                  attrValue.valueName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="250" align="center">
            <template slot-scope="{row,$index}">
              <el-button type="warning" icon='el-icon-edit' size="mini" @click="UpdateAttr(row)">修改</el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加/修改属性 -->
      <div v-show="!isShowTable">
        <el-form label-width="80px" :inline="true" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input placeholder="请输入属性名" v-model="attrInfo.attrName"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" icon="el-icon-plus" @click="addAttrValue"
          :disabled="!attrInfo.attrName">添加属性值</el-button>
        <el-button @click="isShowTable = true">取消</el-button>
        <el-table style="width: 100%" border :data="attrInfo.attrValueList">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="" label="属性值名称" width="width" align="center">
            <template slot-scope="{row,$index}">
              <el-input v-model="row.valueName" placeholder="请输入属性值名称" size="mini" v-if="row.flag" @blur="tolook(row)"
                @keyup.native.enter="tolook(row)" :ref="$index"></el-input>
              <span v-else @click="toEdit(row, $index)" style="display:block">{{ row.valueName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="" label="操作" width="width" align="center">
            <template slot-scope="{row,$index}">
              <!-- 气泡确认框 -->
              <el-popconfirm :title="`确定删除${row.valueName}吗?`" @onConfirm="deleteAttrValue($index)">
                <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="AddOrUpdateAttr" :disabled="attrInfo.attrValueList.length<1">保存</el-button>
        <el-button @click="isShowTable = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from 'lodash/cloneDeep'
export default {
  name: 'attr',
  data() {
    return {
      // 获取子组件传递的categoryId值
      category1Id: '',
      category2Id: '',
      category3Id: '',
      // 获取根据id值发请求得到的属性列表
      AttrList: [],
      // 控制表格显示和隐藏
      isShowTable: true,
      // 收集新增属性|修改属性
      attrInfo: {
        attrName: '',
        attrValueList: [
          // {
          //   attrId: '',
          //   ValueName: ''
          // },
        ],
        // 三级分类的id
        categoryId: 0,
        categoryLevel: 0
      },
    }
  },
  methods: {
    // 获取子组件的categoryId
    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId
      } else if (level == 2) {
        this.category2Id = categoryId
      } else {
        this.category3Id = categoryId
        this.getAttrList(this.category1Id, this.category2Id, this.category3Id)
      }
    },
    // 获取品牌基本属性
    async getAttrList() {
      let result = await this.$API.attr.reqAttrInfoList(this.category1Id, this.category2Id, this.category3Id)
      if (result.code == 200) {
        this.AttrList = result.data
      }
    },
    // 添加属性值
    addAttrValue() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id, //属性的id
        ValueName: '',
        // 用于操作查看和修改
        flag: true
      }),
        this.$nextTick(() => {
          this.$refs[this.attrInfo.attrValueList.length - 1].focus()
        })
    },
    // 添加属性按钮的回调
    addAttr() {
      this.isShowTable = false
      // 清除数据
      this.attrInfo = {
        attrName: '',
        attrValueList: [
        ],
        // 三级分类的id
        categoryId: this.category3Id,
        categoryLevel: 0
      }
    },
    // 修改属性按钮的回调
    UpdateAttr(row) {
      this.isShowTable = false
      // 需要深拷贝
      this.attrInfo = cloneDeep(row)
      // 相应式添加flag属性
      this.attrInfo.attrValueList.forEach(item => {
        this.$set(item, 'flag', false)
      })
    },
    // 切换修改和查看
    tolook(row) {
      // 输入的属性值如果为空则提示
      row.valueName = row.valueName || ''
      if (row.valueName.trim() == '') {
        this.$message('请输入一个正确的属性值')
        return
      }
      // 输入的属性值不能和原来的重复
      let isRepeat = this.attrInfo.attrValueList.some(item => {
        // 需要将row从判断中去除
        if (row != item) {
          return row.valueName == item.valueName
        }
      })

      row.flag = false
    },
    // 切换修改和查看
    toEdit(row, index) {
      row.flag = true
      this.$nextTick(() => {
        this.$refs[index].focus()
      })
    },
    // 删除属性值->气泡框的确认按钮
    deleteAttrValue(index) {
      this.attrInfo.attrValueList.splice(index, 1)
    },
    // 保存(添加或修改)
    async AddOrUpdateAttr() {
      // 整理参数
      // 空的属性值不应该提交
      // 提交时应该把flag字段去掉
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(item => {
        // 过滤掉属性值为空的
        if (item.valueName != '') {
          delete item.flag
          return true
        }
      })
      // 发送请求
      try {
        await this.$API.attr.reqAddOrUpdateAttr(this.attrInfo)
        this.$message({ type: 'success', message: '保存成功' })
        // 保存成功后需要再次获取平台属性进行展示
        this.getAttrList()
        this.isShowTable = true
      } catch (error) {

      }
    }
  }
}
</script>

<style></style>